import Vue from 'vue'
import Router from 'vue-router'
import Login from '../components/Admin/AdminLogin.vue'
import Home from '../components/Admin/AdminHome.vue'
import Welcome from '../components/Admin/AdminWelcome.vue'
import GoodsSell from '../components/Admin/Goods/GoodsSell.vue'
import GoodsBuy from '../components/Admin/Goods/GoodsBuy.vue'
import UserControl from '../components/Admin/UsersControl/UserControl.vue'
import AdminControl from '../components/Admin/UsersControl/AdminControl.vue'

Vue.use(Router)

const routes = [
  { path: '/', redirect: '/admin' },
  { path: '/admin', component: Login },
  {
    path: '/home',
    component: Home,
    redirect: '/welcome',
    children: [
      { path: '/welcome', component: Welcome },
      { path: '/userControl', component: UserControl },
      { path: '/adminControl', component: AdminControl },
      { path: '/goodsSell', component: GoodsSell },
      { path: '/goodsBuy', component: GoodsBuy }
    ] }
]

const router = new Router({
  routes
})
// 挂载一个路由导航守卫
router.beforeEach((to, from, next) => {
  // to 将要访问的路径
  // from 从哪个路径跳转而来
  // next 是一个函数，表示放行
  if (to.path === '/admin') {
    window.sessionStorage.clear()
    return next()
  }
  // 获取token
  const tokenStr = window.sessionStorage.getItem('TokenKey')
  if (!tokenStr) return next('/admin')
  next()
})
export default router
